package net.anzhuo.controller.db.report;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.entity.PaginationVO;
import net.anzhuo.common.utils.RedisUtil;
import net.anzhuo.entity.GsOpenCourt;
import net.anzhuo.entity.db.OpenCourtInfoResult;
import net.anzhuo.entity.db.OpenCourtResponeInfoResult;
import net.anzhuo.entity.db.OpenCourtResponeResult;
import net.anzhuo.entity.db.OpenCourtResult;
import net.anzhuo.entity.db.Paging;
import net.anzhuo.service.GsBaseService;
import net.anzhuo.service.GsOpenCourtService;
import net.sf.json.JSONObject;
/**
 * 
 * @author jiyk
 *   查询开庭公告
 */
@RestController
@Slf4j
public class KaitingController {

	@Autowired
	private GsOpenCourtService gsOpenCourtService;
	@Autowired
	private GsBaseService gsBaseService;
	
	@RequestMapping("/CourtAnnoV4/SearchCourtNotice")
	public OpenCourtResponeResult SearchCourtNotice(String type,String searchKey,String isExactlySame,String pageIndex, String pageSize) {
		log.info("===>getKaiting_searchKey="+searchKey+", pageSize="+pageSize);
		long startTime=System.currentTimeMillis();
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "50";
		}
		
		OpenCourtResponeResult rr = null;
		try {
			rr = (OpenCourtResponeResult)RedisUtil.get("SearchCourtNotice:"+searchKey);
			if (rr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" SearchCourtNotice 执行时间："+excTime+"s");
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		GsOpenCourt gsOpenCourtQuery = new GsOpenCourt();
	//	GsBase base = gsBaseService.getByName(searchKey);
		gsOpenCourtQuery.setCoMd5(searchKey);
		gsOpenCourtQuery.setPageSize(Integer.parseInt(pageSize));
		PaginationVO<GsOpenCourt> gsOpenCourtList = gsOpenCourtService.getListWithPage(gsOpenCourtQuery);
		System.out.println("keyno="+searchKey+",openCourtList="+gsOpenCourtList.getList().size());
		log.info("keyno="+searchKey+",openCourtList="+gsOpenCourtList.getList().size());
		OpenCourtResponeResult responseResult = new OpenCourtResponeResult();
		responseResult.setStatus("200");
		responseResult.setMessage("查询成功");
		
		Paging paging = new Paging();
		paging.setPageIndex(1);
		paging.setPageSize(Integer.parseInt(pageSize));
		paging.setTotalRecords(gsOpenCourtList.getTotalCount());
		responseResult.setPaging(paging);
		
		List<OpenCourtResult> resultList = new ArrayList<OpenCourtResult>();
		OpenCourtResult result = null;
		if (gsOpenCourtList.getList()!=null && gsOpenCourtList.getList().size()>0) {
			for (GsOpenCourt g : gsOpenCourtList.getList()) {
				result = new OpenCourtResult();
				result.setDefendantlist(g.getDefendantlist());
				result.setExecutegov(g.getExecutegov());
				result.setCaseReason(g.getCaseReason());
				result.setId(g.getMd5());
				result.setLianDate(g.getLianDate());
				result.setProsecutorlist(g.getProsecutorlist());
				result.setCaseNo(g.getCaseNo());
				resultList.add(result);
			}
		}
		responseResult.setResult(resultList);
		
		try {
			RedisUtil.set("SearchCourtNotice:"+searchKey, responseResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" SearchCourtNotice 执行时间："+excTime+"s");
		return responseResult;
	}
	
	@RequestMapping("/CourtAnnoV4/GetCourtNoticeInfo")
	public OpenCourtResponeInfoResult GetCourtNoticeInfo(String id,String dtype) {
		log.info("===>getKaitinginfo_searchKey="+id);
		
		
		OpenCourtResponeInfoResult rr = null;
		try {
			rr = (OpenCourtResponeInfoResult)RedisUtil.get("getKaitinginfo:GetCourtNoticeInfo:"+id);
			if (rr != null) {
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		GsOpenCourt gsOpenCourtQuery = new GsOpenCourt();
		 gsOpenCourtQuery = gsOpenCourtService.getByMd5(id);
		
		OpenCourtResponeInfoResult responseResult = new OpenCourtResponeInfoResult();
		responseResult.setStatus("200");
		responseResult.setMessage("查询成功");
		
		
		 JSONObject jsonObject=JSONObject.fromObject(gsOpenCourtQuery.getResult());
		 OpenCourtInfoResult stu=(OpenCourtInfoResult)JSONObject.toBean(jsonObject, OpenCourtInfoResult.class);
		responseResult.setResult(stu);
		
		try {
			RedisUtil.set("getKaitinginfo:GetCourtNoticeInfo:"+id, responseResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return responseResult;
	}
	
	
	
}
